package com.njcky.utils.njssmx;


import com.njcky.model.TreeNode;
import com.njcky.model.dto.SysInParamPo;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @CreateDate 2020/5/13 21:40
 * @Author lcy
 * @Version 1.0.0
 */
public class TreeUtil {
    /**
     * 两层循环实现建树
     *
     * @param treeNodes 传入的树节点列表
     * @return
     */
//    public static <T extends TreeNode> List<T> bulid(List<T> treeNodes, Object root) {
//
//        List<T> trees = new ArrayList<>();
//
//        for (T treeNode : treeNodes) {
//
//            if (root.equals(treeNode.getParentId())) {
//                trees.add(treeNode);
//            }
//
//            for (T it : treeNodes) {
//                if (it.getParentId() == treeNode.getId()) {
//  if (treeNode.getChildren() == null) {
//      treeNode.setChildren(new ArrayList<>());
//  }
//  treeNode.add(it);
//                }
//            }
//        }
//        return trees;

    public static <T extends TreeNode> List<SysInParamPo> bulid(List<SysInParamPo> treeNodes, Object root) {

        List<SysInParamPo> trees = new ArrayList<>();

        for (SysInParamPo treeNode : treeNodes) {
            if (root.equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }
            for (SysInParamPo it : treeNodes) {
                if (it.getParentId().equals(treeNode.getId())) {
  if (treeNode.getChildren() == null) {
      treeNode.setChildren(new ArrayList<>());
  }else {
      treeNode.getChildren().add(it);
  }
                }
            }
        }
        return trees;
    }


	public static List<SysInParamPo> buildTree(List<SysInParamPo> list, Object root){
		Map<Integer, List<SysInParamPo>> groupMap = list.stream().collect(Collectors.groupingBy(SysInParamPo::getParentId));
		list.forEach(organization-> {
			organization.setChildren(groupMap.get(organization.getId()));
		});
		List<SysInParamPo> collect = list.stream().filter(organization-> organization.getParentId().equals(root)).collect(Collectors.toList());
		for (SysInParamPo info :collect) {

			if (!ObjectUtils.isEmpty(info.getChildren())){
				List<SysInParamPo> list1 = info.getChildren().stream().sorted(Comparator.comparing(SysInParamPo::getSort)).collect(Collectors.toList());
				list1.forEach(info1->{
					info1.setParentName(info.getValue());
					if (!ObjectUtils.isEmpty(info1.getChildren())){
						List<SysInParamPo> list2 = info1.getChildren().stream().sorted(Comparator.comparing(SysInParamPo::getSort)).collect(Collectors.toList());
						info1.setChildren(list2);

						for (SysInParamPo inParamPo :info1.getChildren()){
							if (!ObjectUtils.isEmpty(inParamPo.getChildren())){
								List<SysInParamPo> list3 = inParamPo.getChildren().stream().sorted(Comparator.comparing(SysInParamPo::getSort)).collect(Collectors.toList());
								inParamPo.setChildren(list3);
							}
						}
					}
				});
				info.setChildren(list1);
			}
		}
		return collect.stream().sorted(Comparator.comparing(SysInParamPo::getSort)).collect(Collectors.toList());

	}


    /**
     * 使用递归方法建树
     *
     * @param treeNodes
     * @return
     */
    public static <T extends SysInParamPo> List<T> buildByRecursive(List<T> treeNodes, Object root) {
        List<T> trees = new ArrayList<T>();
        for (T treeNode : treeNodes) {
            if (root.equals(treeNode.getParentId())) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    public static <T extends SysInParamPo> T findChildren(T treeNode, List<T> treeNodes) {
        for (T it : treeNodes) {
            if (treeNode.getId() == it.getParentId()) {
                if (treeNode.getChildren() == null) {
  treeNode.setChildren(new ArrayList<>());
                }
                treeNode.add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }
}
